package cn.com.generaldata.os.service.indicator_evaluation.prosperity_analysis;

import org.apache.commons.lang3.Validate;
import org.nutz.dao.sql.Criteria;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.jsme_indicator_system.util.Constants;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.util.BeanMapper;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 主要海洋产业增加值可比价增速评估指标计算类.
 */
public class GOPDeflatorCalculateService extends IndicatorEvaluationCalculateService {

	/** 主要海洋产业增加值 */
	private String majorMarineAdd = ProvinceIndicatorConstant.MAJOR_MARINE_ADDVALUE_NAME;
	/** 地区生产总值平减指数 */
	private String GDPDeflator = ProvinceIndicatorConstant.GDPDEFLATOR;

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double majorMarineAddValue = getInputOrAccountingIndicatorValue(indicatorAccountVO,
				indicatorAccountVO.getYear(), majorMarineAdd);
		Validate.isTrue(majorMarineAddValue != null);
		IndicatorAccountVO iav = BeanMapper.map(indicatorAccountVO, IndicatorAccountVO.class);
		iav.setYear(indicatorAccountVO.getYear() - 1);
		Double majorMarineAddValueLast = getInputOrAccountingIndicatorValue(iav, iav.getYear(), majorMarineAdd);
		Validate.isTrue(majorMarineAddValueLast != null);
		Double evaluationValue = getEvaluationValue(indicatorAccountVO, GDPDeflator);
		Double evaluationValueLast = getEvaluationValue(iav, GDPDeflator);
		return (majorMarineAddValue / evaluationValue) / (majorMarineAddValueLast / evaluationValueLast) - 1;
	}

	

	private Double getEvaluationValue(IndicatorAccountVO indicatorAccountVO, String name) {
		Integer indicatorInputId = getIndicatorEvaluationId(name, indicatorAccountVO.getAreaId());

		Criteria cri = getIndicatorDataCri(indicatorAccountVO, indicatorAccountVO.getYear(), indicatorInputId);
		Double evaluationIndicatorValue = getEvaluationIndicatorValue(cri);
		Validate.isTrue(evaluationIndicatorValue != null);
		return evaluationIndicatorValue;

	}
}
